package pe.com.ValoracionVivienda.views.mantenimiento.ArquitecturasRed;

import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;

import pe.com.ValoracionVivienda.domain.entities.ArquitecturaRNA;
import pe.com.ValoracionVivienda.service.impl.ArquitecturaRNAServiceJpaImpl;
import pe.com.ValoracionVivienda.service.interfaces.ArquitecturaRNAService;
import pe.com.ValoracionVivienda.views.resource.shared.SP3ResourceShared;

public class CompArquitecturasRed extends Composite {
	private Table table;
	private TableViewer tableViewer;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public CompArquitecturasRed(Composite parent, int style) {
		super(parent, style);
		setLayout(new GridLayout(1, false));
		
		ToolBar toolBar = new ToolBar(this, SWT.FLAT | SWT.RIGHT);
		toolBar.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		ToolItem tltmNuevo = new ToolItem(toolBar, SWT.NONE);
		tltmNuevo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				menuNuevo();
			}
		});
		tltmNuevo.setImage(SWTResourceManager.getImage(CompArquitecturasRed.class, "/pe/com/ValoracionVivienda/images/files_add.gif"));
		tltmNuevo.setText(SP3ResourceShared.getString("Nuevo"));
		
		ToolItem tltmEditar = new ToolItem(toolBar, SWT.NONE);
		tltmEditar.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				menuEditar();
			}
		});
		tltmEditar.setImage(SWTResourceManager.getImage(CompArquitecturasRed.class, "/pe/com/ValoracionVivienda/images/files_edit.gif"));
		tltmEditar.setText(SP3ResourceShared.getString("Editar"));
		
		ToolItem tltmActualizar = new ToolItem(toolBar, SWT.NONE);
		tltmActualizar.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				menuActualizar();
			}
		});
		tltmActualizar.setImage(SWTResourceManager.getImage(CompArquitecturasRed.class, "/pe/com/ValoracionVivienda/images/refresh.png"));
		tltmActualizar.setText(SP3ResourceShared.getString("Actualizar"));
		
		table = new Table(this, SWT.BORDER | SWT.FULL_SELECTION);
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn tblclmnNombre = new TableColumn(table, SWT.CENTER);
		tblclmnNombre.setWidth(163);
		tblclmnNombre.setText("Nombre");
		
		TableColumn tblclmnCapaOculta = new TableColumn(table, SWT.CENTER);
		tblclmnCapaOculta.setWidth(107);
		tblclmnCapaOculta.setText("Capa oculta");
		
		TableColumn tblclmnLearningRate = new TableColumn(table, SWT.CENTER);
		tblclmnLearningRate.setWidth(100);
		tblclmnLearningRate.setText("Learning rate");
		
		TableColumn tblclmnMomentum = new TableColumn(table, SWT.CENTER);
		tblclmnMomentum.setWidth(100);
		tblclmnMomentum.setText("Momentum");
		
		TableColumn tblclmnTrainingTime = new TableColumn(table, SWT.CENTER);
		tblclmnTrainingTime.setWidth(100);
		tblclmnTrainingTime.setText("Training Time");
		
		TableColumn tblclmnEstado = new TableColumn(table, SWT.CENTER);
		tblclmnEstado.setWidth(100);
		tblclmnEstado.setText("Estado");

		tableViewer = new TableViewer(table);
		tableViewer.setContentProvider(new IStructuredContentProvider() {
			
			@Override
			public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
				System.out.println("Input changed: old=" + oldInput + ", new=" + newInput);
			}
			
			@Override
			public void dispose() {
				System.out.println("Disposing... TableViewer");
			}
			
			@Override
			public Object[] getElements(Object inputElement) {
				ArquitecturaRNA[] arquitecturas = (ArquitecturaRNA[]) inputElement;
				return arquitecturas;
			}
		});
		tableViewer.setLabelProvider(new ITableLabelProvider() {
			
			@Override
			public void removeListener(ILabelProviderListener arg0) {
			}
			
			@Override
			public boolean isLabelProperty(Object arg0, String arg1) {
				return false;
			}
			
			@Override
			public void dispose() {
			}
			
			@Override
			public void addListener(ILabelProviderListener arg0) {
			}
			
			@Override
			public String getColumnText(Object element, int columnIndex) {
				ArquitecturaRNA arquitecturaRNA = (ArquitecturaRNA) element;
				switch (columnIndex) {
				case 0:
					return arquitecturaRNA.getNombre();
				case 1:
					return arquitecturaRNA.getCapaOculta();
				case 2:
					return Float.toString(arquitecturaRNA.getLearningRate());
				case 3:
					return Float.toString(arquitecturaRNA.getMomentum());
				case 4:
					return Integer.toString(arquitecturaRNA.getTrainingTime());
				case 5:
					return arquitecturaRNA.getEstado() ? SP3ResourceShared.getString("Activo") : SP3ResourceShared.getString("Inactivo");
				}
				
				return null;
			}
			
			@Override
			public Image getColumnImage(Object arg0, int arg1) {
				return null;
			}
		});
	}

	protected void menuActualizar() {
		initData();
	}

	protected void menuEditar() {
		IStructuredSelection selection = (IStructuredSelection) tableViewer.getSelection();
		ArquitecturaRNA arquitecturaRNA = (ArquitecturaRNA)selection.getFirstElement();
		
		if (arquitecturaRNA == null) {
			MessageBox messageBox = new MessageBox(this.getShell(), SWT.ICON_WARNING | SWT.OK);
			messageBox.setText(SP3ResourceShared.getString("SP3"));
			messageBox.setMessage(SP3ResourceShared.getString("Debe_seleccionar") + " una Arquitectura de RNA");
			messageBox.open();
			return ;
		}
			
		ShellArquitecturaRedDetalle shell = new ShellArquitecturaRedDetalle(getDisplay());
		shell.initData(arquitecturaRNA);
		shell.open();
		
		while (!shell.isDisposed()) {
			if (!shell.getDisplay().readAndDispatch()) {
				shell.getDisplay().sleep();
			}
		}
		
		boolean huboCambios = shell.getHuboCambios();
		shell.dispose();
		
		if(huboCambios)
			initData();
	}

	protected void menuNuevo() {
		ShellArquitecturaRedDetalle shell = new ShellArquitecturaRedDetalle(getDisplay());
		shell.initData(new ArquitecturaRNA());
		shell.open();
		while (!shell.isDisposed()) {
			if (!shell.getDisplay().readAndDispatch()) {
				shell.getDisplay().sleep();
			}
		}
		
		boolean huboCambios = shell.getHuboCambios();
		shell.dispose();
		
		if(huboCambios)
			initData();		
	}
	
	public void initData() {
		ArquitecturaRNA[] arquitecturasRNA = null;
		ArquitecturaRNAService arquitecturaRNAService = new ArquitecturaRNAServiceJpaImpl();
		
		arquitecturasRNA = arquitecturaRNAService.findAll().toArray(new ArquitecturaRNA[0]);
		arquitecturaRNAService.close();
		tableViewer.setInput(arquitecturasRNA);
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
